package top.kpromise.common.base;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

@ToString
@Getter
@Setter
@Accessors(chain = true)
public class PageInfo {

    @ApiModelProperty(name = "pageNo", value = "当前页码，从1开始")
    private Integer pageNo;

    @ApiModelProperty(name = "pageSize", value = "每页条数，大于等于1")
    private Integer pageSize;

    @ApiModelProperty(name = "total", value = "总条数")
    private Long total;

    @ApiModelProperty(name = "totalPage", value = "总页数")
    private Long totalPage;

    private void init() {
        if (pageNo == null || pageNo < 1 || pageSize == null || pageSize < 1) {
            this.pageNo = 1;
            this.pageSize = 20;
        }
    }

    @JsonIgnore
    public Integer getFrom() {
        init();
        return (pageNo - 1) * pageSize;
    }

    @JsonIgnore
    public Integer getLimit() {
        init();
        return pageSize;
    }

    public PageInfo setTotal(long total) {
        this.total = total;
        this.totalPage = total / pageSize;
        if (total % pageSize != 0) {
            this.totalPage++;
        }
        return this;
    }
}
